/**
 * Created by admin on 2017/7/16.
 */
function getQueryString(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);console.log( r );
    if(r!=null)return  (r[2]); return null;
}

function brands(  ){
    var url = WEB_URL + '/brands';
    $.get( url, {}, function( data ){
        $('#a1').html('<option value="0">选择品牌</option>');
        for( var i = 0; i < data.length; i ++ ){
            $('#a1').append('<option value="' + data[i].id + '">' + data[i].letter_pre + ' ' + data[i].name +  '</option>');
        }


    },'json');
}

function models(){

    var brand_id= $('#a1').val();
    var url = WEB_URL + '/brand/'+ brand_id + '/models';


    $.get( url, {}, function( data ){

        $('#a2').html('<option value="0">选择车型</option>');
        for( var i = 0; i < data.length; i ++ ){
            $('#a2').append('<option value="' + data[i].model_id + '">'
                + data[i].model_name
                    + '  '
                + data[i].factory_name
                +  '</option>');
        }
    },'json');
}


var modelYears = [];
function getModelYear( objects ){
    modelYears = [];
    $('#a3').html('<option value="0">生产年份/款</option>');
    for( var i = 0; i < objects.length; i ++ ){
        var existed = 0;
        for(var j = 0; j < modelYears.length; j ++){
            if( modelYears[j] == objects[i].year ){
                existed = 1;
                break;
            }
        }
        if( existed == 0){
            $('#a3').append('<option value="'+objects[i].year+'">'+objects[i].year+'</option>');
            modelYears.push( objects[i].year );
        }
    }
}
var modelEngine_displacement = [];
function getModelEngineDisplacement(){
    modelEngine_displacement = [];
    var selectedYear = $('#a3').val();
    $('#a4').html('<option value="0">请选择排量</option>');
    for( var i = 0; i < objects.length; i ++ ){
        var existed = 0;
        for(var j = 0; j < modelEngine_displacement.length; j ++){
            if( modelEngine_displacement[j] == objects[i].engine_displacement ){
                existed = 1;
                break;
            }
        }
        if( existed == 0 ){
            if( selectedYear == 0 || selectedYear == objects[i].year ){
                $('#a4').append('<option value="'+objects[i].engine_displacement+'">'+objects[i].engine_displacement+'</option>');
                modelEngine_displacement.push( objects[i].engine_displacement );
            }
        }
    }
}

var objects=[];
function modelObjects(){

    var model_id = $('#a2').val();
    var url = WEB_URL + '/model/'+ model_id + '/objects';
    $.get( url, {}, function( data ){
        objects = data;
        $('#a5').html('<option value="0">选择具体车型</option>');
        for( var i = 0; i < data.length; i ++ ){
            $('#a5').append('<option value="' + data[i].mi_key + '">'
                + data[i].label
                + '  '
                + data[i].year
                + '  '
                + data[i].engine_displacement
                +  '</option>');

        }

        getModelYear( objects );
        getModelEngineDisplacement( objects );
    },'json');

}

function select_objects( step ){

    var selectedYear = $('#a3').val();
    if( step == 'year' ){
        getModelEngineDisplacement();
    }
    var selectedEngine_displacement = $('#a4').val();
    $('#a5').html('<option value="0">选择具体车型</option>');
    for( var i = 0; i < objects.length; i ++ ){
        if( selectedEngine_displacement==0 || objects[i].engine_displacement == selectedEngine_displacement ){
            if( selectedYear==0 || objects[i].year == selectedYear ){
                $('#a5').append('<option value="' + objects[i].mi_key + '">'
                    + objects[i].label
                    + '  '
                    + objects[i].year
                    + '  '
                    + objects[i].engine_displacement
                    +  '</option>');
            }
        }
    }

}

function pipei(){
    var obj = document.getElementById("a5"); //selectid

    var index = obj.selectedIndex;

    var text = obj.options[index].text;

    var mi_key = obj.options[index].value;
    //var car_id=$('#car_id').val();
    if(!mi_key || mi_key==0){
        alert('请选择具体车型！');
        return;
    }
    // window.location.href='result.html?mi_key='+mi_key;
    window.location.href='detail.html?mi_key='+mi_key;
}

/********************************************************/
var extra_data = []; //juhe2 来源数据的extra_data
function load_extra_data( data ){
    $('#extra-data').show();
    extra_data = data;
    var options = '';
    for( var i = 0; i < extra_data.length; i ++ ){
        var item = extra_data[i];
        options += '<option value="' + item.id + '">'+ item.year + ' ' + item.gearbox + '</option>';
    }
    $('#model-select').html( options );
    $('#model-select').change(function(  ){
        var extra_id = $(this).val();
        for(  var i = 0; i < extra_data.length; i ++  ){
            if( extra_data[i].id == extra_id ){
                var selected_model = extra_data[i];
                $('#year').html( selected_model.year );
                $('#gearbox_type').html( selected_model.gearbox );
                $('#gearbox_lv').html( selected_model.gearbox_lv );
                $('#oil_img_url').attr( 'src', IMG_URL + 'images/' + selected_model.oil_img_url );
                $('#oil-name').html( selected_model.oil_name );
            }
        }

    });
}

function objectInfo(){
    var model_id = $('#a2').val();
    var mi_key = getQueryString('mi_key');
    var vin = getQueryString('vin');
    var url = WEB_URL + '/objectInfo/mi_key/'+ mi_key;
    if(  mi_key == null || mi_key=='' ){
        if( vin != null && vin.replace(/^(\s+)|(\s+)$/,'').length == 17 ){
            url = WEB_URL + '/objectInfo/vin/'+ vin.replace(/^(\s+)|(\s+)$/,'');
        }else{
            alert('您请求的页面不合法');
            return;
        }
    }

    $.get( url, {}, function( data ){
        $('#mask').hide();
        if( Object.prototype.toString.call(data)=="[object Object]" ){

            $('#oil,#oil-name').html( data.oil );
            $('#oil_regional').html( data.oil_regional );
            $('#oil_change').html( data.oil_change );
            $('#oil-link').attr('href', data.link );
            $('#oil_img_url').attr( 'src', IMG_URL + 'images/' + data.oil_img_url );
            $('#gearbox_lv').html( data.gearbox_lv );
            if(data.gearbox_type != undefined ){
                $('#gearbox_type').html( data.gearbox_type );
            }else{
                $('#gearbox_type').html( data.gearbox );
            }
            $('#engine_displacement').html( data.engine_displacement );
            $('#year').html( data.year );
            $('#model_name').html( data.model_name );
            $('#factory_name').html( data.factory_name );
            $('#label').html( data.label );
            $('#engine').html( data.engine );
            if( url.indexOf('vin') > -1 ){
                $('#title').html( data.factory_name + ' ' + data.model_name + ' ' + data.year + ' ' + data.model_type );
            }else{
                $('#title').html( data.brand_name + ' ' + data.model_name + ' ' + data.year + ' ' + data.label );
            }

            //todo 如果extra_data 非空，并且数量大于1，添加模型选择功能
            console.log( data );
            if( data.source=='juhe2' && data.extra_data.length > 1 ){
                load_extra_data( data.extra_data );
            }

        }else{
            alert('抱歉，该VIN码不存在');
        }

    },'json');
}



